package com.usian.controller;

import com.usian.feign.OSSServiceFeign;
import com.usian.pojo.TbUser;
import com.usian.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author lpf
 * @Date 2022/3/26 上午11:26
 * @ProjectName java134-e-commerce-project
 * @Version 1.0
 * @Description:
 */
@RestController
@RequestMapping(value = "frontend/sso")
public class SSOController {

    @Autowired
    private OSSServiceFeign ossServiceFeign;

    /**
     * 用于校验用户名和注册手机号是否可用（未被注册）接口
     * @param checkValue
     * @param checkFlag
     * @return
     * @Auth lpf
     */
    @RequestMapping(value = "checkUserInfo/{checkValue}/{checkFlag}")
    public Result checkUserInfo(@PathVariable("checkValue") String checkValue, @PathVariable("checkFlag") Integer checkFlag){
        Boolean result = ossServiceFeign.checkUserInfo(checkValue, checkFlag);
        if (result){
            return Result.ok();
        }
        return Result.error("校验失败！");
    }

    /**
     * 用户注册接口
     * @param tbUser
     * @return
     * @Auth lpf
     */
    @RequestMapping(value = "userRegister")
    public Result userRegister(TbUser tbUser){
        Integer result = ossServiceFeign.userRegister(tbUser);
        if (result == 1){
            return Result.ok();
        }
        return Result.error("注册失败！");
    }

    /**
     * 用户登录接口
     * @param username
     * @param password
     * @return
     * @Auth lpf
     */
    @RequestMapping(value = "userLogin")
    public Result userLogin(String username, String password){
        Map resultMap = ossServiceFeign.userLogin(username, password);
        if (resultMap != null && resultMap.size() > 0){
            return Result.ok(resultMap);
        }
        return Result.error("登录失败！");
    }

    /**
     * 获取用户token接口
     * @param token
     * @return
     * @Auth lpf
     */
    @RequestMapping(value = "getUserByToken/{token}")
    public Result getUserByToken(@PathVariable("token") String token){
        TbUser tbUser = ossServiceFeign.getUserByToken(token);
        if (tbUser != null){
            return Result.ok();
        }
        return Result.error("登录失败");
    }

    /**
     * 注销登录接口
     * @param token
     * @return
     * @Auth lpf
     */
    @RequestMapping(value = "logOut")
    public Result logOut(@RequestParam String token){
        Boolean logout = ossServiceFeign.logOut(token);
        if (logout){
            return Result.ok();
        }
        return Result.error("注销失败");
    }
}
